﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.Custom;
using DWF.Studio.Host;
using DWF.Studio.Project;
using DWF.Studio.RemoteCtrls.Custom;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace DWF.Studio.Views
{
    /// <summary>
    /// 活动包工具栏控件
    /// </summary>
    public partial class UCtrlActivityToolBox : UserControl
    {
        /// <summary>
        /// 远程工具栏宿主
        /// </summary>
        private RemoteToolBoxHost _toolBoxHost = null;

        public UCtrlActivityToolBox()
        {
            InitializeComponent();

            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                init();
            }
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void init()
        {
            GlobalActionHandler.ProjectOpened += loadToolBox;
            GlobalActionHandler.ProjectClosing += closeToolbox;

            GlobalActionHandler.PackageChanging += closeToolbox;
            GlobalActionHandler.PackageChanged += loadToolBox;
        }

        /// <summary>
        /// 加载工具栏
        /// </summary>
        /// <param name="e"></param>
        private void loadToolBox(EventArgs e)
        {
            var projectManager = ProjectManager.Instance;
            var project = projectManager.CurrentProject;//当前工程

            var loaderHost = RemoteLoaderHost.GetCurrentInstance();//获取当前远程控件加载器
            var libpath = System.IO.Path.Combine(project.FullName, StaticResource.ProjectSelfLibs);
            var activitypathes = projectManager.GetDependOnPackageInfos().Where(item => item.IsInstalled).Select(item => System.IO.Path.Combine(new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).Parent.FullName, StaticResource.Activities, String.Format(@"{0}.{1}", item.Id, item.Version), StaticResource.ActivitiesLib, item.TargetFramework)).ToList();//获取工程依赖活动包信息
            activitypathes.Add(libpath);

            WindowLoading.SetContent(LocalizationProvider.GetLocalizedString("WindowLoading_Msg_ProbeActivity"));
            var activities = RemoteLoaderHost.ProbeActivity(activitypathes.ToArray());//探测活动

            WindowLoading.SetContent(LocalizationProvider.GetLocalizedString("WindowLoading_Msg_LoadActivity"));
            _toolBoxHost = new RemoteToolBoxHost(loaderHost.LoadToolBoxProxy(), activities);
            borderToolboxBorder.Child = _toolBoxHost.GetToolBox();//获取工具栏
        }
        /// <summary>
        /// 关闭项目
        /// </summary>
        /// <param name="e"></param>
        private void closeToolbox(EventArgs e)
        {
            borderToolboxBorder.Child = null;
            _toolBoxHost?.Dispose();
            _toolBoxHost = null;
        }

        public void Refresh()
        {
            this.borderToolboxBorder.Child?.Refresh();
        }
    }
}
